package tt.dz.egobus.servicemanager.core.task;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.bean.BaseBean;
import tt.dz.egobus.servicemanager.business.convert.DynamicTaskConvert;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.task.job.QuartzDynamicTask;
import tt.dz.egobus.vo.KV;

import java.util.*;

/**
 * @author Anson
 * @date 17-8-4
 * @name 动态任务实体
 * @since 1.0.6
 */
public class DynamicTask extends DynamicTaskNamespace implements BaseBean, Comparable<DynamicTask> {

    private static final long serialVersionUID = 5690096962389436077L;

    /**
     * 唯一性
     */
    private String jobClassName;
    private Date startTime;
    private List<KV<String, String>> jobDatas;


    public static Converter<BusinessCache, DynamicTask> jobNameConvert(Class<? extends QuartzDynamicTask> clazz,
                                                                       Date startTime) {
        return new DynamicTaskConvert(clazz, startTime);
    }


    public DynamicTaskNamespace toNameSpace() {
        DynamicTaskNamespace namespace = new DynamicTaskNamespace(jobName, jobGroupName, triggerName, triggerGroupName);
        return namespace;
    }

    public static List<String> convertTaskJson(List<DynamicTask> tasks) {
        if (tasks == null || tasks.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<String> taskJsons = new ArrayList<String>();
        String taskJson = null;
        for (DynamicTask t : tasks) {
            taskJson = t.toJsonStr();
            taskJsons.add(taskJson);
        }
        return taskJsons;
    }

    public static DynamicTask jsonStrConvertTask(String jsonTask) throws JSONException {
        return JSONObject.parseObject(jsonTask, DynamicTask.class);
    }

    public String toJsonStr() {
        return JSONObject.toJSONString(this);
    }

    public static List<DynamicTask> convertTask(List<String> taskStrs) {
        if (taskStrs == null || taskStrs.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<DynamicTask> tasks = new ArrayList<DynamicTask>();
        DynamicTask task = null;
        for (String dynamicTaskStr : taskStrs) {
            try {
                task = DynamicTask.jsonStrConvertTask(dynamicTaskStr);
            } catch (JSONException e) {
                continue;
            }
            tasks.add(task);
        }
        return tasks;
    }

    public static Map<String, DynamicTask> convertTaskMap(List<String> taskStrs) {
        if (taskStrs == null || taskStrs.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        List<DynamicTask> tasks = DynamicTask.convertTask(taskStrs);
        return DynamicTask.dynamicTaskListToMap(tasks);
    }

    public static Map<String, DynamicTask> dynamicTaskListToMap(final List<DynamicTask> tasks) {
        if (tasks != null && !tasks.isEmpty()) {
            String key = null;
            Map<String, DynamicTask> map = new HashMap<String, DynamicTask>();
            for (DynamicTask task : tasks) {
                key = task.key();
                map.putIfAbsent(key, task);
            }
            return map;
        } else {
            return Collections.EMPTY_MAP;
        }
    }


    /**
     * @author Anson
     * @date 17-8-9
     * @name 当前转换为任务的动态数据绑定
     * @since 1.0.6
     */
    public Map<String, Object> convertJobDataMap() {
        List<KV<String, String>> jds = this.getJobDatas();
        if (jds == null || jds.isEmpty()) {
            return Collections.EMPTY_MAP;
        }

        Map<String, Object> jobDataMap = new HashMap<>();
        for (KV<String, String> kv : jds) {
            jobDataMap.putIfAbsent(kv.getK(), kv.getV());
        }
        return jobDataMap;
    }


    public Class jobClass() {
        if (StringUtils.isBlank(this.jobClassName)) {
            return null;
        }
        try {
            Class clazz = Class.forName(this.jobClassName);
            return clazz;
        } catch (ClassNotFoundException e) {
            return null;
        }
    }


    public String getJobClassName() {
        return jobClassName;
    }

    public void setJobClassName(String jobClassName) {
        this.jobClassName = jobClassName;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public List<KV<String, String>> getJobDatas() {
        return jobDatas;
    }

    public void setJobDatas(List<KV<String, String>> jobDatas) {
        this.jobDatas = jobDatas;
    }

    @Override
    public int hashCode() {
        return this.key().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !this.getClass().isAssignableFrom(obj.getClass())) {
            return false;
        }
        DynamicTask task = (DynamicTask) obj;
        return this.hashCode() == task.hashCode();
    }

    @Override
    public String toString() {
        return "DynamicTask{" +
                "key='" + this.key() + '\'' +
                ", jobName='" + jobName + '\'' +
                ", jobGroupName='" + jobGroupName + '\'' +
                ", triggerName='" + triggerName + '\'' +
                ", triggerGroupName='" + triggerGroupName + '\'' +
                ", jobClassName='" + jobClassName + '\'' +
                ", startTime=" + startTime +
                '}';
    }

    @Override
    public int compareTo(DynamicTask o) {
        return o == null ? 1 : this.key().compareTo(o.key());
    }
}
